###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/CreateLanguageSet.pm.pl -> CreateLanguageSet module            #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::CreateLanguageSet;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";

use File::Find;
use File::Copy;
use Cwd;
use strict;

require "ControlPanel/Output/CreateLanguageSet.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, ERROR => "", @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "LANGUAGESETS_CREATE");

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my ($LanguageSets, $LanguageSetsIndex) = $in{'DB'}->Query(
    TABLE   => "LanguageSets",
    SORT    => "DESCRIPTION",
    BY      => "A-Z"
  );

  my %INPUT;
  
  $INPUT{'LANGUAGESETS'} = $LanguageSets;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::CreateLanguageSet->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT, error => $in{'ERROR'});
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "LANGUAGESETS_CREATE");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  my @Fields = (
    { name => "ID"         , required => 1, size => 64  },
    { name => "DESCRIPTION", required => 1, size => 256 },
    { name => "LANGUAGESET", required => 1              }
  );

  my (%RECORD, %INPUT, @Error);

  foreach my $field (@Fields) {
    if ($field->{'required'} && $SL::QUERY{'FORM_'.$field->{'name'}} eq "") {
      push(@Error, "MISSING-".$field->{'name'});
    } elsif ($field->{'size'} && $SL::QUERY{'FORM_'.$field->{'name'}} ne "" && length($SL::QUERY{'FORM_'.$field->{'name'}}) > $field->{'size'}) {
      push(@Error, "TOOLONG-".$field->{'name'});
    } else {
      $RECORD{ $field->{'name'} } = $SL::QUERY{'FORM_'.$field->{'name'}};
    }
  }

  if ($RECORD{'ID'} && $in{'DB'}->BinarySelect(TABLE => "LanguageSets", KEY => $RECORD{'ID'})) {
    push(@Error, "ALREADYEXISTS-ID");
  }

  if ($RECORD{'LANGUAGESET'}) {
    my $languageset = $in{'DB'}->BinarySelect(
      TABLE => "LanguageSets",
      KEY   => $RECORD{'LANGUAGESET'}
    );
    if ($languageset) {
      $INPUT{'LANGUAGESET'} = $languageset;
    } else {
      push(@Error, "INVALID-LANGUAGESET");
    }
  }

  if (scalar(@Error) >= 1) {
    $self->show(DB => $in{'DB'}, ERROR => \@Error);
    return 1;
  }

  #----------------------------------------------------------------------#
  # Inserting data...                                                    #

  delete($RECORD{'LANGUAGESET'});

  (
    $in{'DB'}->Insert(
      TABLE   => "LanguageSets",
      VALUES  => \%RECORD
    )
  ) || &Error::Error("CP", MESSAGE => "Error inserting record. $in{'DB'}->{'ERROR'}");
  
  $RECORD{'LANGUAGESET'} = $INPUT{'LANGUAGESET'}->{'ID'};

  mkdir("$SL::PATH/Private/Skins/Language/$RECORD{'ID'}", 0777)
    || &Error::CGIError("Error creating language set directory. $!", "$SL::PATH/Private/Skins/Language/$RECORD{'ID'}");
  chmod(0777, "$SL::PATH/Private/Skins/Language/$RECORD{'ID'}");
  
  my $Cwd = cwd();
  
  my (@Files, @Directories);
  my $FindSub = eval {
    sub {
      if (-f) {
        push(@Files, $File::Find::name);
      } elsif (-d && $_ !~ /^\.\.?$/) {
        push(@Directories, $File::Find::name);
      }
    }
  };

  chdir("$SL::PATH/Private/Skins/Language/$RECORD{'LANGUAGESET'}");
  find(\&$FindSub, ".");
  chdir($Cwd);
  
  foreach my $directory (sort @Directories) {
    $directory =~ s/^\.\///;
    mkdir("$SL::PATH/Private/Skins/Language/$RECORD{'ID'}/$directory", 0777) || &Error::CGIError("Error creating directory. $!", "$SL::PATH/Private/Skins/Language/$RECORD{'ID'}/$directory");
    chmod(0777, "$SL::PATH/Private/Skins/Language/$RECORD{'ID'}/$directory");
  }
  foreach my $file (@Files) {
    $file =~ s/^\.\///;
    copy("$SL::PATH/Private/Skins/Language/$RECORD{'LANGUAGESET'}/$file", "$SL::PATH/Private/Skins/Language/$RECORD{'ID'}/$file") || &Error::CGIError("Error copying file. $!", "$SL::PATH/Private/Skins/Language/$RECORD{'LANGUAGESET'}/$file");
    chmod(0666, "$SL::PATH/Private/Skins/Language/$RECORD{'ID'}/$file");
  }

  $INPUT{'RECORD'} = \%RECORD;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::CreateLanguageSet->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;